package ink.metoo.auto.fishpi.chatroom.command

import ink.metoo.auto.fishpi.Log
import ink.metoo.auto.fishpi.call.ChatRoomCall
import ink.metoo.auto.fishpi.chatroom.Message

class QuteMessageCommand : Command {

    override val commandText: String = "/引用"

    override fun isMatch(message: Message): Boolean {
        return super.isMatch(message) && message.isMe
    }

    override fun execute(message: Message) {
        try {
            val commandParams = message.commandParams
            val oId = commandParams.getOrNull(1) ?: return
            val messageRaw = ChatRoomCall.getContextMessage(oId = oId, mode = 1, size = 1, type = "md").data[0]
            val content = messageRaw.content
            if (content.isNullOrBlank()) {
                return
            }
            val oldMessageRaw = content.split("\n").joinToString("\n") { "> $it" }
            val quteMsg = message.md!!.split(oId)[1].trim()
            ChatRoomCall.sendMessage("$quteMsg \n##### 引用 @${messageRaw.userName} [↩](https://fishpi.cn/cr#chatroom${oId} \"跳转至原消息\") \n$oldMessageRaw\n")
        } catch (e: Exception) {
            Log.debug(e.message, e)
        }

    }

}